package com.liantong.demo_part2.Controller;

import com.liantong.demo_part2.Service.PONResourceService;
import com.liantong.demo_part2.Utils.JsonResult;
import com.liantong.demo_part2.Utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;


@Controller
@RequestMapping("/Access/PONResource")
@Api(tags = "PON利用率统计文档")
public class PONResourceController {

    private Result result = new Result();

    @Autowired
    private PONResourceService ponResourceService;



    @RequestMapping(value = "/PON_board_rate", method = RequestMethod.GET)
    @ApiOperation(value = "统计不同分公司、OLT下的PON板资源利用率（槽位利用率）", response = Result.class)
    @ApiImplicitParams({

            @ApiImplicitParam(name = "department", value = "(选填)选择哪个区域", paramType = "query",  dataType = "String", defaultValue = "南开通信分公司"),
            @ApiImplicitParam(name = "olt", value = "(选填)选择哪个OLT", paramType = "query",  dataType = "String", defaultValue = "西营门局OLT15-C300")
    })
    public void PONBoardRate(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String department = request.getParameter("department");
        String olt = request.getParameter("olt");
        if ("".equals(department)) {
            department = null;
        }
        if ("".equals(olt)) {
            olt = null;
        }
        try {
            Map<String, Object> res = ponResourceService.PON_board_rate(department,olt);
            if (res != null) {
                result.packetResult(true, "返回成功", res);
                JsonResult.toJson(result, response);
            } else {
                result.packetResult(false, "数据为空", null);
                JsonResult.toJson(result, response);
            }
        } catch (Exception e) {
            result.packetResult(false, null, null);
            JsonResult.toJson(result, response);
            response.setStatus(400);
        }

    }



    @RequestMapping(value = "/PON_port_rate", method = RequestMethod.GET)
    @ApiOperation(value = "统计不同分公司、OLT下的PON口资源利用率（在已使用的板上）", response = Result.class)
    @ApiImplicitParams({

            @ApiImplicitParam(name = "department", value = "(选填)选择哪个区域", paramType = "query",  dataType = "String", defaultValue = "南开通信分公司"),
            @ApiImplicitParam(name = "olt", value = "(选填)选择哪个OLT", paramType = "query",  dataType = "String", defaultValue = "西营门局OLT15-C300")
    })
    public void PON_port_rate(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String department = request.getParameter("department");
        String olt = request.getParameter("olt");
        if ("".equals(department)) {
            department = null;
        }
        if ("".equals(olt)) {
            olt = null;
        }
        try {
            Map<String, Object> res = ponResourceService.PON_port_rate(department,olt);
            if (res != null) {
                result.packetResult(true, "返回成功", res);
                JsonResult.toJson(result, response);
            } else {
                result.packetResult(false, "数据为空", null);
                JsonResult.toJson(result, response);
            }
        } catch (Exception e) {
            result.packetResult(false, null, null);
            JsonResult.toJson(result, response);
            response.setStatus(400);
        }
    }


    @RequestMapping(value = "/totalRate", method = RequestMethod.GET)
    @ApiOperation(value = "统计不同分公司、OLT下的PON口资源利用率（在已使用的板上）和PON板资源利用率（槽位利用率）", response = Result.class)
    @ApiImplicitParams({

            @ApiImplicitParam(name = "department", value = "(选填)选择哪个区域", paramType = "query",  dataType = "String", defaultValue = "南开通信分公司"),
            @ApiImplicitParam(name = "olt", value = "(选填)选择哪个OLT", paramType = "query",  dataType = "String", defaultValue = "西营门局OLT15-C300")
    })
    public void totalRate(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String department = request.getParameter("department");
        String olt = request.getParameter("olt");
        Map<String,Map<String, Object>> res=new HashMap<>();
        try {
            Map<String, Object> port_rate = ponResourceService.PON_port_rate(department,olt);
            Map<String, Object> board_rate = ponResourceService.PON_board_rate(department,olt);
            if (port_rate != null && board_rate != null) {
                res.put("port_rate", port_rate);
                res.put("board_rate", board_rate);
            }
            if (res != null) {
                result.packetResult(true, "返回成功", res);
                JsonResult.toJson(result, response);
            } else {
                result.packetResult(false, "数据为空", null);
                JsonResult.toJson(result, response);
            }
        } catch (Exception e) {
            result.packetResult(false, null, null);
            JsonResult.toJson(result, response);
            response.setStatus(400);
        }
    }


    @RequestMapping(value = "/returnDepartment", method = RequestMethod.GET)
    @ApiOperation(value = "查询所有区域信息", response = Result.class)
    public void returnDepartment(HttpServletRequest request, HttpServletResponse response) throws Exception {
        try {
            List<String> list = ponResourceService.returnDepartment();
            if (list != null) {
                result.packetResult(true, "返回成功", list);
                JsonResult.toJson(result, response);
            } else {
                result.packetResult(false, "数据为空", null);
                JsonResult.toJson(result, response);
            }
        } catch (Exception e) {
            result.packetResult(false, null, null);
            JsonResult.toJson(result, response);
            response.setStatus(400);
        }
    }

    @RequestMapping(value = "/returnOLT", method = RequestMethod.GET)
    @ApiOperation(value = "查询某个区域的全部OLT信息", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "department", value = "(必填)选择哪个表", paramType = "query", required = true, dataType = "String", defaultValue = "南开通信分公司", example = "南开通信分公司")
    })
    public void returnOLT(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String department = request.getParameter("department");
        try {
            List<String> list = ponResourceService.returnOLT(department);
            if (list != null) {
                result.packetResult(true, "返回成功", list);
                JsonResult.toJson(result, response);
            } else {
                result.packetResult(false, "数据为空", null);
                JsonResult.toJson(result, response);
            }
        } catch (Exception e) {
            result.packetResult(false, null, null);
            JsonResult.toJson(result, response);
            response.setStatus(400);
        }
    }

    @RequestMapping(value = "/returnTree", method = RequestMethod.GET)
    @ApiOperation(value = "返回树状图信息", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "department", value = "(必填)选择哪个局站", paramType = "query", required = true, dataType = "String", defaultValue = "河北", example = "河北")
    })
    public void returnTree(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String department = request.getParameter("department").trim()+"通信分公司";
        try {
            Map<String,Object> list = ponResourceService.returnTree(department);
            if (list != null) {
                result.packetResult(true, "返回成功", list);
                JsonResult.toJson(result, response);
            } else {
                result.packetResult(false, "数据为空", null);
                JsonResult.toJson(result, response);
            }
        } catch (Exception e) {
            result.packetResult(false, null, null);
            JsonResult.toJson(result, response);
            response.setStatus(400);
        }
    }

}
